//!CompilerOption:AddRef:BothavenManager.dll
using System;
using System.IO;
using System.Reflection;
using System.Linq;
using Styx.Plugins;

namespace BothavenManagerLoaders.Plugins
{
    public class Plugin39 : HBPlugin
    {
        static Plugin39()
        {
            BothavenManager.BothavenManager.User.CheckForUpdates(39);
            var filename = 39 + ".dll";
            try
            {
                var buffer = File.ReadAllBytes(Path.Combine(PluginFolder, filename));
                Assembly pluginAssembly = Assembly.Load(buffer);
                var nc = pluginAssembly.GetTypes().FirstOrDefault(t => typeof(HBPlugin).IsAssignableFrom(t));
                PluginInstance = (HBPlugin)pluginAssembly.CreateInstance(nc.FullName);
                success = PluginInstance != null;
            }
            catch (Exception)
            {
                success = false;
            }
        }

        private static readonly HBPlugin PluginInstance;
        private static string PluginFolder
        {
            get { return Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "Plugins", "botHavenManager Plugins"); }
        }

        public override void Initialize()
        {
            if (success)
                PluginInstance.Initialize();
        }

        public override void Pulse()
        {
            if (success)
                PluginInstance.Pulse();
        }

        private static bool success;
        public override string Name
        {
            get
            {
                return success ? PluginInstance.Name : "[botHaven] {productName} failed to load";
            }
        }
        public override string Author
        {
            get { return success ? PluginInstance.Author : "botHaven"; }
        }

        public override Version Version
        {
            get { return success ? PluginInstance.Version : new Version(1, 0); }
        }
        public override string ButtonText
        {
            get
            {
                return success ? PluginInstance.ButtonText : base.ButtonText;
            }
        }
        public override void OnButtonPress()
        {
            if (success)
                PluginInstance.OnButtonPress();
        }
        public override bool WantButton
        {
            get
            {
                return success ? PluginInstance.WantButton : base.WantButton;
            }
        }
        public override void Dispose()
        {
            if (success)
                PluginInstance.Dispose();
            else
                base.Dispose();
        }
        public override bool Equals(object obj)
        {
            return success ? PluginInstance.Equals(obj) : base.Equals(obj);
        }
        public override int GetHashCode()
        {
            return success ? PluginInstance.GetHashCode() : base.GetHashCode();
        }
        public override string ToString()
        {
            return success ? PluginInstance.ToString() : base.ToString();
        }
    }
}